package com.itheima;

import com.ibm.icu.impl.CalendarAstronomer;
import com.itheima.pojo.User;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

// 使用样式，边框线，对齐方式，字体，行高，合格单元格 多个样式的处理 案例
public class Demo1 {
    public static void main(String[] args) throws Exception{
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("有样式的数据");
        // 设置列宽
        sheet.setColumnWidth(0,5*256);
        sheet.setColumnWidth(1,8*256);
        sheet.setColumnWidth(2,15*256);
        sheet.setColumnWidth(3,16*256);
        sheet.setColumnWidth(4,30*256);
        // 需求： 边框线 行高 合并单元格 对齐方式 字体
        XSSFCellStyle bigTitleRowCellStyle = workbook.createCellStyle();

        bigTitleRowCellStyle.setBorderBottom(BorderStyle.THIN);  // 下边框   thin细线
        bigTitleRowCellStyle.setBorderLeft(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderRight(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderTop(BorderStyle.THIN);
        // 对齐方式 水平居中，垂直居中
        bigTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER);  // 水平居中
        bigTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        // 创建字体
        XSSFFont font = workbook.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 18);
        // 把字体放入样式中
        bigTitleRowCellStyle.setFont(font);

        XSSFRow bigTitleRow = sheet.createRow(0);
        bigTitleRow.setHeightInPoints(42);  // 设置行高
        for (int i = 0; i < 5; i++) {
            XSSFCell cell = bigTitleRow.createCell(i);
            cell.setCellStyle(bigTitleRowCellStyle);
        }
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));  // int firstRow 起始行, int lastRow 结束行, int firstCol 起始列, int lastCol 结束列
        // 第一行插入数据
        sheet.getRow(0).getCell(0).setCellValue("用户信息数据");

        // 小标题
        XSSFCellStyle littleTitleRowCellStyle = workbook.createCellStyle();
        littleTitleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle); // 样式的克隆

        // 创建字体
        XSSFFont littleFont = workbook.createFont();
        littleFont.setFontName("宋体");
        littleFont.setFontHeightInPoints((short) 12);
        littleFont.setBold(true);
        // 把字体放入样式中
        littleTitleRowCellStyle.setFont(littleFont);

        // 内容区
        XSSFCellStyle contentRowCellStyle = workbook.createCellStyle();
        contentRowCellStyle.cloneStyleFrom(littleTitleRowCellStyle); // 样式的克隆
        contentRowCellStyle.setAlignment(HorizontalAlignment.LEFT);

        // 创建字体
        XSSFFont contentFont = workbook.createFont();
        contentFont.setFontName("宋体");
        contentFont.setFontHeightInPoints((short) 11);
        contentFont.setBold(false);
        // 把字体放入样式中
        contentRowCellStyle.setFont(contentFont);

        // 标题内容
        String[] titles = new String[]{"编号","姓名","手机号","入职日期","现住址"};
        XSSFRow row = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(littleTitleRowCellStyle);
        }



        // 内容
        String[] users = new String[]{"1","大一1","1231212","2012-01-12","郑州"};
        for (int i = 2; i < 100; i++) {
            XSSFRow rowContent = sheet.createRow(i);
            for (int i1 = 0; i1 < 5; i1++) {
                XSSFCell cell = rowContent.createCell(i1);
                cell.setCellValue(users[i1]);
                cell.setCellStyle(contentRowCellStyle);
            }
        }

        workbook.write(Files.newOutputStream(Paths.get("H:\\LUA\\spring_report\\workspace\\user_management\\src\\main\\resources\\testStyle.xlsx")));

    }
}
